package common;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.laoxinwen.app.R;

import java.io.File;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Field;
import java.util.HashMap;

import util.MyAnimator;
import util.bitmap.FileUtil;
import util.bitmap.Img;
import util.bitmap.ImgPool;

public class BaseFragment extends Fragment {

    //图片相关
    protected File cacheDir = FileUtil.getFileDir();
    protected File fileDir = FileUtil.getCacheDir();
    protected HashMap<String, Img> imgMap = new HashMap<>();
    protected ImgPool imgPool = new ImgPool() {
        @Override
        public void bitmapDecoded(Img<?> img, View bindedView, boolean isFistDecode) {
            ImageView iv = (ImageView) bindedView;
            iv.setImageBitmap(img.getBitmap());
            if (isFistDecode) {
                MyAnimator.alphaListPic(bindedView);
            }
        }

        @Override
        public void bitmapNotDecoded(View bindedView) {
            ImageView iv = (ImageView) bindedView;
            iv.setImageResource(R.drawable.default_img_bg_large);
        }

        @Override
        public void bind(Img<?> img, View view) {
            if(UserConfig.isNoPic()) {
                return;
            }
            super.bind(img, view);
        }
    };
    protected Img getImg(String url) {
        Img img = imgMap.get(url);
        if (null == img) {
            img = new Img(cacheDir, fileDir, url);
            imgMap.put(url, img);
        }
        return img;
    }

    //
    //设置data
    //
    protected Bundle bundle;

    public void setBundle(Bundle bundle){
        this.bundle = bundle;
    }

    @Override
    public void onPause() {
        super.onPause();
        imgPool.releaseSomeMemory();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        imgPool.shutdown();
    }

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        getActivity().overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
    }

    @Override
    public void startActivityForResult(Intent intent, int requestCode) {
        super.startActivityForResult(intent, requestCode);
        getActivity().overridePendingTransition(android.R.anim.slide_out_right, android.R.anim.slide_in_left);
    }

    //
    //根据id找view相关
    //
    @Target(ElementType.FIELD)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface id {
        int value();
    }

    public <T extends View> T f(int id, View v) {
        return (T) v.findViewById(id);
    }

    public void loadView(View rootView) {
        Field[] fields = getClass().getDeclaredFields();
        try {
            for (Field field : fields) {
                if (field.isAnnotationPresent(BaseFragment.id.class)) {
                    field.setAccessible(true);
                    field.set(this, rootView.findViewById(field.getAnnotation(BaseFragment.id.class).value()));
                }
            }
        } catch (Exception e) {
            Log.e(BaseFragment.class.getName(), e.toString());
        }
    }
}
